import multiprocessing
import threading
import time


def p_main(q_msg_out, q_img_out, q_cmd_in, q_cmd_out):
    from Flight_Single.Flight_connection_mutliprocessing import Connection
    from Flight_Single.Flight_video_deal import Video_deal
    from Flight_Single.Flight_message_deal import Massage_deal
    con = Connection(cmd_in=q_cmd_in, cmd_out=q_cmd_out)
    msg = Massage_deal(connection=con, msg_queue=q_msg_out)
    video = Video_deal(connection=con, img_queue=q_img_out)
    con.send(cmd="command")
    print(" *** ", con.rec_result())
    con.send(cmd='downvision {}'.format(0))
    print(" *** ", con.rec_result())
    con.send(cmd='setfps {}'.format('middle'))
    print(" *** ", con.rec_result())
    con.send(cmd='setbitrate {}'.format(1))
    print(" *** ", con.rec_result())
    video.start()
    msg.start()
    con.start()
    con.cmd_thread.join()


def p_img_deal(img_in, b_img_out, o_img_out, rst_out):
    from Detection.Find_tool2 import Tool
    tool = Tool(img_in, b_img_out, o_img_out, rst_out)
    tool.start()
    # tool.start_show()
    tool.deal_thread.join()


def p_HUD_web(img_in, msg_in, b_img_in, out_img_in, cmd_in):
    from HUD.HUD_control_multiprocessing2 import HUD_control
    from Flight_Single.Web_Connection import Web_Connection

    HUD = HUD_control()
    Web = Web_Connection()

    def get_msg_in():
        while True:
            msg = msg_in.get()
            Web.message = msg
            HUD.set_msg(msg)

    def get_deal_img_in():
        while True:
            Web.p_img = b_img_in.get()
            Web.d_img = out_img_in.get()

    def get_img_in():
        while True:
            img = img_in.get()
            HUD.set_img(img)
            Web.HUD_img = HUD.get_deal_img()

    def get_cmd_in():
        while True:
            Web.command_queue = cmd_in.get()

    # HUD.start_show()

    msg_in_thread = threading.Thread(target=get_msg_in)
    deal_in_thread = threading.Thread(target=get_deal_img_in)
    img_in_thread = threading.Thread(target=get_img_in)
    cmd_in_thread = threading.Thread(target=get_cmd_in)

    msg_in_thread.start()
    deal_in_thread.start()
    img_in_thread.start()
    cmd_in_thread.start()

    Web.run()

    msg_in_thread.join()


def p_control(rst_in, cmd_in, cmd_out):
    from Control.Mission_final import Mission
    mission = Mission(rst=rst_in, cmd_in=cmd_in, cmd_out=cmd_out)
    # time.sleep(10)
    # cmd_in.put(['takeoff', 1])
    # cmd_out.get()
    # time.sleep(0.5)
    # cmd_in.put(['cw 90', 1])
    # cmd_out.get()
    # time.sleep(0.5)
    # cmd_in.put(['ccw 90', 1])
    # cmd_out.get()
    # time.sleep(0.5)
    # cmd_in.put(['ccw 90', 1])
    # cmd_out.get()
    # cmd_in.put(['go -60, -60, 80, 60, m12', 1])
    # cmd_out.get()
    # cmd_in.put(['land', 1])
    # cmd_out.get()
    mission.start()


if __name__ == '__main__':
    q_main_msg = multiprocessing.Queue()
    q_main_img_out = multiprocessing.Queue()
    q_main_cmd_in = multiprocessing.Queue()
    q_cmd_control = multiprocessing.Queue()
    q_cmd_web = multiprocessing.Queue()
    q_main_cmd_out = multiprocessing.Queue()
    q_rst = multiprocessing.Queue(maxsize=2)
    q_b_img = multiprocessing.Queue()
    q_o_img = multiprocessing.Queue()

    q_img_web_hud_in = multiprocessing.Queue()
    q_img_deal_in = multiprocessing.Queue()

    main_process = multiprocessing.Process(target=p_main,
                                           args=(q_main_msg, q_main_img_out, q_cmd_control, q_main_cmd_out))
    img_process = multiprocessing.Process(target=p_img_deal, args=(q_img_deal_in, q_b_img, q_o_img, q_rst))
    HUD_web_process = multiprocessing.Process(target=p_HUD_web,
                                              args=(q_img_web_hud_in, q_main_msg, q_b_img, q_o_img, q_cmd_web))
    control_process = multiprocessing.Process(target=p_control, args=(q_rst, q_main_cmd_in, q_main_cmd_out))


    def img_update():
        while True:
            img = q_main_img_out.get()
            q_img_web_hud_in.put(img)
            q_img_deal_in.put(img)


    def cmd_update():
        while True:
            cmd = q_main_cmd_in.get()
            q_cmd_control.put(cmd)
            q_cmd_web.put(cmd)


    img_thread = threading.Thread(target=img_update)
    cmd_thread = threading.Thread(target=cmd_update)

    img_thread.start()
    cmd_thread.start()

    main_process.start()
    img_process.start()
    HUD_web_process.start()
    control_process.start()

    img_thread.join()
